﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MacomberMapSystem.Common.Serialization;
using MacomberMapSystem.Common.CIM;
using System.Data.Common;
using MacomberMapSystem.Common.Database;

namespace MacomberMapSystem.Common.Integration.Blackstart
{
    /// <summary>
    /// This class holds information on a synchroscope
    /// </summary>
    public class MM_Synchroscope: MM_Serializable
    {
        #region Variable declarations
        /// <summary>The rdf:ID of our synchroscope</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]        
        public CIM_RdfID RdfID;

        /// <summary>The substation or company for our synchroscope</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public String Substation;

        /// <summary>The name of our element</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]                
        public String Name;

        /// <summary>The type of our element</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]                
        public String ElemType;

        /// <summary>Whether the synchroscope is matched up with the model</summary>
        public bool Matched = false;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new synchroscope
        /// </summary>
        /// <param name="dRd"></param>
        /// <param name="Repository"></param>
        public MM_Synchroscope(DbDataReader dRd, MM_Repository Repository)
            : base(dRd, null, Repository)
        {

        }

        /// <summary>
        /// Initialize a new synchroscope 
        /// </summary>
        /// <param name="Substation"></param>
        /// <param name="Element"></param>
        public MM_Synchroscope(CIM_Element Substation, CIM_Element Element): base(null,null)
        {
            this.Substation = Substation.Name;
            this.RdfID = Element.rdfID;
            this.ElemType = Element.ElemType;
            this.Name = Element.Name;
            this.Matched = true;
        }

        /// <summary>
        /// Initialize a new synchroscope and merge it with the model
        /// </summary>
        /// <param name="dRd"></param>
        /// <param name="Repository"></param>
        /// <param name="Model"></param>
        public MM_Synchroscope(DbDataReader dRd, MM_Repository Repository, MM_Database_Model Model)
            : this(dRd, Repository)
        {
            if (RdfID != null)
                Matched = Model.FromRdfID(RdfID) != null;                    
        }

        /// <summary>
        /// Initialize a model from the network model
        /// </summary>
        /// <param name="SynchroscopeInfo"></param>
        /// <param name="Model"></param>
        public MM_Synchroscope(String SynchroscopeInfo, MM_Database_Model Model)
            : base(null, null)
        {
            String[] splStr;
            if (!SynchroscopeInfo.Contains(','))
            {
                splStr = SynchroscopeInfo.Split('.');
                this.Substation = splStr[0];
                this.Name = splStr.Length == 1 ? null : splStr[1];
            }
            else
            {
                splStr = SynchroscopeInfo.Split(',');
                this.Substation = splStr[0];
                this.Name = String.IsNullOrEmpty(splStr[2]) ? null : splStr[2]; 
            }

            //First, find our substation
            CIM_Element Substation;
            if (this.Substation.StartsWith("*"))
            {
                if (!Model.Companies.TryGetValue(this.Substation.Substring(1), out Substation))
                    return;
            }
            else if (!Model.Substations.TryGetValue(this.Substation, out Substation))
                return;

            if (String.IsNullOrEmpty(Name))
            {
                ElemType = Substation.ElemType;
                RdfID = Substation.rdfID;
            }
            else
                foreach (CIM_Element Elem in Substation.Elements)
                    if ((Elem.TypeName == "cim:Breaker" || Elem.TypeName == "cim:Disconnector") && String.Equals(Elem.Name, Name, StringComparison.CurrentCultureIgnoreCase))
                    {
                        RdfID = Elem.rdfID;
                        ElemType = Elem.ElemType;
                        Matched = true;
                        return;
                    }
        }
        #endregion

    }
}
